<template>
  <div>
    <div class="itemContainer" v-for="item in list" :key="item.id">
      <img @click="avatarClick(item.userInfo.id)" :src="item.userInfo.avatar_url ? item.userInfo.avatar_url: item.userInfo.sex===1 ? '../../static/defaultAvatar1.jpeg': '../../static/defaultAvatar2.jpeg'" alt="">
      <div>
        <div class="userName" @click="avatarClick(item.userInfo.id)">
          {{item.userInfo.name}}
        </div>
        <div class="description">
          {{item.userInfo.description ? item.userInfo.description : 'TA很懒，还没有添加简介'}}
        </div>
      </div>
      <el-button size="mini" round v-if="item.toType ===1" @click="cancelFocus(item, item.userInfo.id)">已关注</el-button>
      <el-button size="mini" round v-else @click="focus(item, item.userInfo.id)">关注</el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array
    }
  },
  methods: {
    avatarClick (id) {
      this.$router.push(`/profile/${id}`)
    },
    cancelFocus (item, id) {
      this.$confirm('是否取关这位靓仔？', '取关', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$http.post(`/users/relation/${id}/2`)
        item.toType = 0
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    async focus (item, id) {
      await this.$http.post(`/users/relation/${id}/1`)
      this.$nextTick(() => {
        item['toType'] = 1
      })
    }
  },
  mounted () {
    console.log(this.list, 'pppp')
  }
}
</script>

<style scoped lang="less">
  .itemContainer {
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;
    padding: 10px 0;
    .userName:hover {
      cursor: pointer;
      color: #5a9cf8;
    }
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      margin-right: 10px;
      cursor: pointer;
    }
    .userName {
      margin-bottom: 5px;
      color: #222226;
    }
    .description {
      font-size: 14px;
      color:#555666;
      opacity: 0.8;
    }
    .el-button {
      margin-left: auto;
    }
  }
</style>
